/**
 * 
 */
package com.dhcc.DRGsHosp.application.impl;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.FieldMapApplication;
import com.dhcc.DRGsHosp.application.XPathParseApplication;

/**
 * 
 * @ClassName: FieldMapApplicationImpl  
 * @Description ：    
 * @Date: 2018年1月19日 上午10:13:44  
 * @Author mazhaoyan 
 * @Version 2.5.0 DHC-DRGs
 */
@Named
@Transactional
public class XPathParseApplicationImpl implements XPathParseApplication {
	
	@Autowired
	private FieldMapApplication fieldMapApplication;

	/**  
	 *  @Description：
	 *  @Author mazhaoyan  
	 *  @see com.dhcc.DRGsHosp.application.XPathParseApplication#getMrXPathMap(java.lang.String, java.lang.String)  
	 */
	@Override
	public Map<String, Object> getMrXPathMap(String xmlType) {
		Map<String, Object> mrXPathMap = new HashMap<String, Object>();
		// 基本信息
		List<Map<String,Object>> baseXPathMap = fieldMapApplication.getFieldMap(xmlType,"MR_BASEINFO");
		// 住院信息
		List<Map<String,Object>> ipXPathMap = fieldMapApplication.getFieldMap(xmlType,"MR_IPINFO");
		// 附页信息
		List<Map<String,Object>> appendXPathMap = fieldMapApplication.getFieldMap(xmlType,"MR_APPENDPAGE");
		// 费用信息
		List<Map<String,Object>> expXPathMap = fieldMapApplication.getFieldMap(xmlType,"MR_EXPENSE");
		//诊断信息
		List<Map<String,Object>> diagXPathMap = fieldMapApplication.getFieldMap(xmlType,"MR_DIAGNOSE");
		Map<String,List<Map<String,Object>>> diagMap = new HashMap<String,List<Map<String,Object>>>();
		for(int i = 0;i<diagXPathMap.size();i++)
		{
			String order = diagXPathMap.get(i).get("FIELD_ORDER") == null?null:diagXPathMap.get(i).get("FIELD_ORDER").toString();
			Object type = diagXPathMap.get(i).get("FIELD_TYPE");
			//使用特殊字符分则 记录path
			if(type==null)
			{
				//主要诊断和其他诊断均重复
				if(diagMap.get("A")==null)
				{
					List<Map<String,Object>> ol = new LinkedList<Map<String,Object>>();
					ol.add(diagXPathMap.get(i));
					diagMap.put("A",ol);
				}
				else
				{
					diagMap.get("A").add(diagXPathMap.get(i));
				}
			}
			else if("M".equals(type.toString()))
			{//主要诊断
				if(diagMap.get("M")==null)
				{
					List<Map<String,Object>> ol = new LinkedList<Map<String,Object>>();
					ol.add(diagXPathMap.get(i));
					diagMap.put("M",ol);
				}
				else
				{
					diagMap.get("M").add(diagXPathMap.get(i));
				}
			}
			else if("O".equals(type.toString()))
			{//其他诊断
				if("-1".equals(order))
				{//其他诊断重复
					if(diagMap.get("O")==null)
					{
						List<Map<String,Object>> ol = new LinkedList<Map<String,Object>>();
						ol.add(diagXPathMap.get(i));
						diagMap.put("O",ol);
					}
					else
					{
						diagMap.get("O").add(diagXPathMap.get(i));
					}
				}
				else {//其他诊断不重复
					if(diagMap.get(order)==null)
					{//当前序号的还没有存
						List<Map<String,Object>> ol = new LinkedList<Map<String,Object>>();
						ol.add(diagXPathMap.get(i));
						diagMap.put(order,ol);
					}
					else
					{//当前顺序存在
						diagMap.get(order).add(diagXPathMap.get(i));
					}
				}
			}
		}
		List<Map<String,Object>> operXPathMap = fieldMapApplication.getFieldMap(xmlType,"MR_OPERATION");
		Map<String,List<Map<String,Object>>> operMap = new HashMap<String,List<Map<String,Object>>>();
		for(int i = 0;i<operXPathMap.size();i++)
		{
			String order = operXPathMap.get(i).get("FIELD_ORDER")==null?null:operXPathMap.get(i).get("FIELD_ORDER").toString();
			Object type = operXPathMap.get(i).get("FIELD_TYPE");
			//使用特殊字符分则 记录path
			if(type==null)
			{//主要诊断和其他诊断均重复
				if(operMap.get("A")==null)
				{
					List<Map<String,Object>> ol = new LinkedList<Map<String,Object>>();
					ol.add(operXPathMap.get(i));
					operMap.put("A",ol);
				}
				else
				{
					operMap.get("A").add(operXPathMap.get(i));
				}
			}
			else if("M".equals(type.toString()))
			{//主要诊断
				if(operMap.get("M")==null)
				{
					List<Map<String,Object>> ol = new LinkedList<Map<String,Object>>();
					ol.add(operXPathMap.get(i));
					operMap.put("M",ol);
				}
				else
				{
					operMap.get("M").add(operXPathMap.get(i));
				}
			}
			else if("O".equals(type.toString()))
			{//其他诊断
				if("-1".equals(order))
				{//其他诊断重复
					if(operMap.get("O")==null)
					{
						List<Map<String,Object>> ol = new LinkedList<Map<String,Object>>();
						ol.add(operXPathMap.get(i));
						operMap.put("O",ol);
					}
					else
					{
						operMap.get("O").add(operXPathMap.get(i));
					}
				}
				else {//其他诊断不重复
					if(operMap.get(order)==null)
					{//当前序号的还没有存
						List<Map<String,Object>> ol = new LinkedList<Map<String,Object>>();
						ol.add(operXPathMap.get(i));
						operMap.put(order,ol);
					}
					else
					{//当前顺序存在
						operMap.get(order).add(operXPathMap.get(i));
					}
				}
			}
			
		}
		mrXPathMap.put("base",baseXPathMap);
		mrXPathMap.put("ip",ipXPathMap);
		mrXPathMap.put("append",appendXPathMap);
		mrXPathMap.put("exp",expXPathMap);
		mrXPathMap.put("diag",diagMap);
		mrXPathMap.put("oper",operMap);
		return mrXPathMap;
	}
}
